package com.shadow.test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import net.sourceforge.jeval.EvaluationException;
import net.sourceforge.jeval.Evaluator;

public class Server {

	private static final double PRECISION = 0.0001;
	
	private static final List<String> fuhaos = new ArrayList<>();
	
	static {
		fuhaos.add("+");
		fuhaos.add("-");
		fuhaos.add("*");
		fuhaos.add("/");
	}
	
	public static void main(String[] args) throws Exception {
		
//		String file = "C:\\Users\\yangzhuangqiu\\Desktop\\2018.txt";
//		
//		List<String> lines = FileUtils.readLines(new File(file), "utf8");
//		
//		for(String line : lines) {
//			
//			if(StringUtils.isNotBlank(line)) {
//				
//				for(String fh : fuhaos) {
//					
//					if(line.indexOf(fh) == -1) {
//						System.out.println(line);
//					}
//					
//				}
//				
//				
//			}
//			
//		}
		
		test();
		System.out.println("=============");
	}
	
	public static void test() throws Exception {
		Server instance = new Server();
		//数字串
		List<String> list = new ArrayList<>();
		list.add("666666666666");
		//list.add("987654321");
		//目标数字
		int targetNumber = 2018;
		
		for(String number : list){
			instance.start(number, targetNumber);
		}
	}
	
	private List<Integer> getNumbers(String numberStr, String split){
		
		List<Integer> results = new ArrayList<>();
		int beginIndex = 0;
		String [] x = split.split("-");
		for(int i = 0; i < x.length; i ++){
			
			//char c = split.charAt(i);
			
			if(StringUtils.isNotBlank(x[i])){
				int endIndex = Integer.parseInt(x[i]);
				String sub = numberStr.substring(beginIndex, endIndex);
				results.add(Integer.parseInt(sub));
				beginIndex = endIndex;
			}
		}
		String sub = numberStr.substring(beginIndex, numberStr.length());
		results.add(Integer.parseInt(sub));
		return results;
	}
	
	private Set<String> getAllSplit(int n){
		return getAllCsByCount(n);
	}
	
	public void start(String numberStr, int targetNumber) throws EvaluationException{
		
		Evaluator eva = new Evaluator();
		
		Set<String> splits = getAllSplit(numberStr.length() - 1);
		
		for(String split : splits){
			
			try{
				List<Integer> numbers = getNumbers(numberStr, split);
				List<String> results = getAll(numbers);
				
				for(String result : results){
					String tmp = eva.evaluate(result);
					
					double _tmp = Double.parseDouble(tmp);
					
					if(Math.abs(_tmp - targetNumber) < PRECISION){
						System.out.println(targetNumber + "=" + result);
					}
					
				}
				
			}catch(Exception e){
				
			}
			
			
		
		}
		
		
		
		
		
	}
	
	private List<String> getAll(List<Integer> numbers){
		
		
		List<String> inits = new ArrayList<>();
		List<Integer> others = new ArrayList<>();
		for(int i = 0; i < numbers.size(); i ++){
			Integer number = numbers.get(i);
			if(i == 0){
				inits.add("-"+number);
				inits.add(""+number);
			}else{
				others.add(number);
			}
		}
		
		List<String> results = inits; 
		for(Integer other : others){
			results = getMore(results, other);
		}
		return results;
	}
	
	private List<String> getMore(List<String> olds, int number){
		
		List<String> results = new ArrayList<>(olds.size() * fuhaos.size());
		
		for(String old : olds){
			for(String fuha : fuhaos){
				results.add(old + fuha + number);
			}
		}
		
		return results;
		
	}
	
	private Set<String> getAllCsByCount(int count){
		List<Integer> list = new ArrayList<>();
		for(int i = 1; i <= count; i++){
			list.add(i);
		}
		return getAllCs(list);
	}
	
	private Set<String> getAllCs(List<Integer> list){
		
		Set<String> results = new HashSet<>();
		for(int i = 1; i <= list.size(); i ++){
			Set<String> set = getAllCsByNumber(list, i);
			results.addAll(set);
		}
		return results;
	}
	
	
	private Set<String> getAllCsByNumber(List<Integer> list, int number){
		
		Set<String> results = new HashSet<>();
		
		for(Integer i : list){
			results.add("-" + i + "-");
		}
		
		if(1 == number){
			return results;
		}
		
		for(int i = 1; i < number; i ++){
			results = getNewCs(results, list);
		}
		
		return results;
	}
	
	private Set<String> getNewCs(Set<String> olds, List<Integer> list){
		
		Set<String> set = new HashSet<>();
		
		for(String old : olds){
			
			for(Integer i : list){
				
				if(!old.contains("-" + i + "-")){
					set.add(getNewOneC(old + i + "-"));
				}
			}
			
		}
		return set;
	}
	
	//a是一个排列，返回一个组合
	private String getNewOneC(String a){
		
		String[] arr = a.split("-");
		
		//char[] array = a.toCharArray();
		List<Integer> list = new ArrayList<>();
		for(String x : arr){
			if(StringUtils.isNotBlank(x)){
				list.add(Integer.parseInt(x));
			}
		}
		
		Collections.sort(list);
		
		StringBuilder sb = new StringBuilder("-");
		for(int i = 0; i < list.size(); i ++){
			sb.append(list.get(i)).append("-");
		}
		
		return sb.toString();
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
